/**
 * Copyright Dinglian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lyh.provider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 用于获取useLocalCache的值
 *
 * @author LinYeHui
 * @date 2022/5/26
 * @company Dinglian
 */

@RestController
@RefreshScope
/**
 * 读取配置中心useLocalCache的值是否使用本地缓存
 */
public class ProviderCacheController {
    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    private String douseLocalCache() {
        return "useLocalCache's is " + useLocalCache;
    }

    // 当缓存开启时，从缓存中去数据，当缓存没有开启，从数据库取数据
    private CopyOnWriteArrayList<String> localCache = new CopyOnWriteArrayList();
    @GetMapping("/cache2")
    public List<String> getCache02(){
        if(!useLocalCache){
            System.out.println("从数据库取数据");
            return Arrays.asList("a","b","c");
        }
        if(localCache.isEmpty()){
            System.out.println("从缓存取数据");
            List<String> list = Arrays.asList("a", "b", "c");
            localCache.addAll(list);
        }
        return localCache;
    }

}

